/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package trackreggui.swing;

import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.InputEvent;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.TransferHandler;
import webreg.model.Athlet;

/**
 *
 * @author STKNOLL
 */
public class AthletListTransferHandler extends TransferHandler  {
    private Icon mDragIcon;
    
    public AthletListTransferHandler(Icon inIcon) {
        mDragIcon = inIcon;
    }

    @Override
    public boolean canImport(TransferSupport arg0) {
       System.out.println("GUI: AthletListTransferHandler Can Import: " + arg0.getTransferable().getClass() );
        // we only import athlets and disciplins
        if ((arg0.getSourceDropActions()==MOVE) && (arg0.isDataFlavorSupported( AthletTransferable.getFlavor()))) {
            System.out.println("GUI: AthletListTransferHandler Import is possible");
            return true;
        } else
        {
            System.out.println("GUI: AthletListTransferHandler Import not possible");
            return false;
        }
    }

    @Override
    protected Transferable createTransferable(JComponent arg0) {
        System.out.println("GUI: createTransferable athlet");
        Athlet dragObject = null;
        if (arg0 instanceof JList) {
            JList theList = (JList) arg0;
            dragObject = (Athlet)theList.getModel().getElementAt(theList.getSelectedIndex());
            System.out.println("GUI: Selected object: " + dragObject.getClass().getName());
        }
        return new AthletTransferable(dragObject);
    }

    @Override
    public void exportAsDrag(JComponent arg0, InputEvent arg1, int arg2) {
        System.out.println("GUI: exportAsDrag of athlet");
        super.exportAsDrag(arg0, arg1, arg2);       // leave this call here
    }

    @Override
    protected void exportDone(JComponent arg0, Transferable arg1, int arg2) {
        System.out.println("GUI: exportDone of athlet");
        super.exportDone(arg0, arg1, arg2);
    }

    @Override
    public int getSourceActions(JComponent arg0) {
        // has to be copy, tree handler will check
        return COPY;
    }

    @Override
    public Icon getVisualRepresentation(Transferable arg0) {
        System.out.println("GUI: getVisualRepresentation of athlet transferable");
//        return super.getVisualRepresentation(arg0);
        return mDragIcon;
    }

    @Override
    public boolean importData(TransferSupport arg0) {
        // does nothing, throw away the import data
        return true;
    }

}
